'''
Copyright(C),2021-2031, Zhenxin Tech. Co., Ltd.

FilePath: pub.py
Author: zhangys
Date: 2021-12-14 00:09:24
LastEditTime: 2021-12-14 00:11:29
LastEditors: zhangys
Description: 发布者
'''
import rclpy
from rclpy.node import Node
from std_msgs.msg import String

'''
流程：
    1.创建消息发布类
    2.创建消息发布对象
    3.调用阻塞发布
    4.显示销毁节点
    5.关闭发送
'''

class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(String, 'topic', 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = String()
        msg.data = 'Hello ROS2！: %d' % self.i
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%s"' % msg.data)
        self.i += 1


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)
    minimal_publisher.destroy_node()#显示销毁节点
    rclpy.shutdown()


if __name__ == '__main__':
    main()